#!/bin/sh

function module()
{
  if [ -z $1 ]
  then
    echo "Missing parameter in module generator."
    return 1
  fi

  if cd includes/modules 2>/dev/null
  then
    echo "Directory `pwd`/$1.module"
    mkdir $1.module
    if cd $1.module 2>/dev/null
    then
      echo "Directory `pwd`/actions"
      mkdir actions
      echo "Directory `pwd`/views"
      mkdir views
      echo "File `pwd`/$1.module.php"
      touch $1.module.php
      return 0
    else
      echo "Unable to generate module directory."
      return 1
    fi
  else
    echo "Could not change to modules directory."
    return 1
  fi
}

function action()
{
  if [ -z $1 ]
  then
    echo "Missing module parameter in action generator."
    return 1
  fi

  if [ -z $2 ]
  then
    echo "Missing action parameter in action generator."
    return 1
  fi

  if cd includes/modules/$1.module 2>/dev/null
  then
    if cd actions 2>/dev/null
    then
      echo "File `pwd`/$2.action.php"
      touch $2.action.php
      return 0
    else
      echo "Actions directory missing. Generating."
      echo "Directory `pwd`/actions"
      mkdir actions
      if cd actions 2>/dev/null
      then
        echo "File `pwd`/$2.action.php"
        touch $2.action.php
        return 0
      else
        echo "Unable to generate actions directory."
        return 1
      fi
    fi
  else
    echo "Module missing. Generating."
    module $1
    if cd includes/modules/$1.module 2>/dev/null
    then
      if cd actions 2>/dev/null
      then
        echo "File `pwd`/$2.action.php"
        touch $2.action.php
        return 0
      else
        echo "Actions directory missing. Generating."
        echo "Directory `pwd`/actions"
        mkdir actions
        if cd actions 2>/dev/null
        then
          echo "File `pwd`/$2.action.php"
          touch $2.action.php
          return 0
        else
          echo "Unable to generate actions directory."
          return 1
        fi
      fi
    else
      echo "Unable to generate module."
      return 1
    fi
  fi
}

function view()
{
  if [ -z $1 ]
  then
    echo "Missing module parameter in view generator."
    return 1
  fi

  if [ -z $2 ]
  then
    echo "Missing view parameter in view generator."
    return 1
  fi

  if cd includes/modules/$1.module 2>/dev/null
  then
    if cd views 2>/dev/null
    then
      echo "File `pwd`/$2.phtml"
      touch $2.phtml
      return 0
    else
      echo "Views directory missing. Generating."
      echo "Directory `pwd`/views"
      mkdir views
      if cd views 2>/dev/null
      then
        echo "File `pwd`/$2.phtml"
        touch $2.phtml
        return 0
      else
        echo "Unable to generate views directory."
        return 1
      fi
    fi
  else
    echo "Module missing. Generating."
    module $1
    if cd includes/modules/$1.module 2>/dev/null
    then
      if cd views 2>/dev/null
      then
        echo "File `pwd`/$2.phtml"
        touch $2.phtml
        return 0
      else
        echo "Views directory missing. Generating."
        echo "Directory `pwd`/views"
        mkdir views
        if cd views 2>/dev/null
        then
          echo "File `pwd`/$2.phtml"
          touch $2.phtml
          return 0
        else
          echo "Unable to generate views directory."
          return 1
        fi
      fi
    else
      echo "Unable to generate module."
      return 1
    fi
  fi
}

if [ -z $1 ]
then
  echo "Missing parameter for generator type."
  exit
fi

if [ -z $2 ]
then
  echo "Missing parameter for generator name."
  exit
fi

$1 $2 $3
